package com.learn.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn.egweibo2.base.response.JsonResult;
import com.learn.egweibo2.base.response.StatusCode;
import com.learn.egweibo2.mapper.WeiboMapper;
import com.learn.egweibo2.pojo.dto.WeiboParam;
import com.learn.egweibo2.pojo.entity.Weibo;
import com.learn.egweibo2.pojo.vo.UserVO;
import com.learn.egweibo2.pojo.vo.WeiboDetailVO;
import com.learn.egweibo2.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Validated
@RestController
@RequestMapping("/v1/weibo/")
@Slf4j
@Api(tags = "02.微博管理模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("insert")
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboParam weiboParam, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.debug("userVO:"+userVO);
        //判断用户是否登录
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboParam,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());

        int rs = weiboMapper.insertWeibo(weibo);
        log.debug(rs>0?"发布成功":"发布失败");
        return JsonResult.ok();
    }

    @GetMapping("selectIndex")
    @ApiOperation("微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectWeiboList(){
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable weiboNotice = ops.get("Weibo_notice");
        if(weiboNotice!=null){
            return JsonResult.ok(weiboNotice);
        }

        log.debug("selectIndex");
        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectWeiboList();
        ops.set("Weibo_notice",(Serializable) weiboIndexVOS,30, TimeUnit.SECONDS);

        return JsonResult.ok(weiboIndexVOS);
    }

    @GetMapping("selectById")
    @ApiOperation("微博详情也")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "Long", paramType = "query")
    public JsonResult selectWeiboById(@Range(min = 1,max = 100,message = "微博id必须在1-100之间") Long id){
        log.debug("id:"+id);
        if(id<0){
            throw new IllegalArgumentException("id不能小于0");
        }

        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboDetail(id);
        log.debug("weiboDetailVO:"+weiboDetailVO);

        return JsonResult.ok(weiboDetailVO);
    }
}
